package com.yuebao.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yuebao.api.TransactionService;
import com.yuebao.constant.OrderConstants;
import com.yuebao.dto.TransactionDTO;
import com.yuebao.enumeration.ResponseCodeEnum;
import com.yuebao.model.pojo.Transaction;
import com.yuebao.result.BaseResponse;
import com.yuebao.result.EntityResponse;
import com.yuebao.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/order")
public class TransactionController {
    
    @Resource
    TransactionService transactionService;
    
    @PostMapping("/search")
    public TransactionDTO getOrderByOrderNo(@RequestParam("orderNo")  String  orderNo){
        Transaction transaction=transactionService
                .query()
                .eq("order_NO",Long.valueOf(orderNo))
                .one();
        TransactionDTO transactionDTO = BeanUtil.toBean(transaction, TransactionDTO.class);
        log.info("===order==>{}", JsonUtils.writeValueAsString(transactionDTO));
        return transactionDTO;
    }
    
    @PostMapping("/updateState")
    public BaseResponse updateTransactionState(@RequestParam("orderNo")  String  orderNo,@RequestParam("orderState")  String  orderState){
        boolean update = transactionService.update()
                .eq("order_NO", Long.valueOf(orderNo))
                .set("order_state", orderState)
                .update();
        if(!update){
            return new BaseResponse(ResponseCodeEnum.FORBIDDEN,"更新失败");
        }
        return new BaseResponse(ResponseCodeEnum.SUCCESS,"更新状态成功");
    
    }
    
}
